package com.self.cgqcompose.ui.components

import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.indication
import androidx.compose.foundation.interaction.MutableInteractionSource
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxHeight
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.offset
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.wrapContentHeight
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.remember
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import coil.compose.AsyncImage
import com.self.cgqcompose.beans.BottomTabs
import com.self.cgqcompose.beans.Tab
import com.self.cgqcompose.mock.MockJson
import com.self.cgqcompose.ui.composables.CustomBoxWithOverflow

@Composable
fun BottomBar(tabs: BottomTabs, selectedIndex: Int, onItemSelected: (Int) -> Unit) {
    CustomBoxWithOverflow(
        modifier = Modifier
            .height(48.dp).background(Color.White)
            .fillMaxWidth()
    ) {
        Row(
            modifier = Modifier.fillMaxWidth().height(78.dp).
            padding(bottom = 5.dp),
            horizontalArrangement = Arrangement.SpaceEvenly,
            verticalAlignment = Alignment.Bottom,
        ) {
            val interactionSource = remember { MutableInteractionSource() }
            tabs.info.tabBar.tabs.forEachIndexed { index, tab ->
            val size = if (index == 2) 60.dp else 50.dp
                Box(
                    modifier = Modifier
                        .weight(1f)
//                        .fillMaxHeight()
//                        .height(size)
                        .indication(interactionSource, null)
                        .clickable { onItemSelected(index) },

                    contentAlignment = Alignment.Center
                ) {
                    TabItem(tab, index, selectedIndex)
                }
            }
        }
    }
}

@Composable
fun TabItem(tab: Tab, index: Int, selectedIndex: Int) {
    Column(horizontalAlignment = Alignment.CenterHorizontally) {
        val size = if (index == 2) 60.dp else 27.dp
        val offsetY = if (index == 2) (2).dp else 0.dp
        val iconUrl = if (index == selectedIndex) tab.selectedIcon else tab.icon
        AsyncImage(
            model = iconUrl, // 假设 tab 对象有一个 iconUrl 属性
            contentDescription = "tab icon",
            modifier = Modifier
//                .padding(top = 3.dp)
                .offset(y = offsetY)
                .size(size) // 根据需要调整修饰符
        )
        if (index != 2) {
            Text(
                text = tab.name,
                fontSize = 10.sp,
                modifier = Modifier
                    ,
                textAlign = TextAlign.Center,
                style = TextStyle(lineHeight = 10.sp)
            )
        }
    }
}

@Preview(showBackground = true)
@Composable
fun BottomBarPreview() {
    val context = LocalContext.current;
    val tabs = MockJson.loadMockDataBottomTabs(context)
    BottomBar(tabs, 0) {}
}